package com.magic.gre.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.flyco.tablayout.SlidingTabLayout;
import com.magic.gre.R;
import com.magic.gre.adapter.PagerAdapterHome;
import com.magic.gre.base.activity.BaseCenterActivity;
import com.magic.gre.helper.Apphelper;
import com.magic.gre.ui.fragment.AccountLoginFragment;
import com.magic.gre.ui.fragment.PhoneLoginFragment;
import com.noname.lib_base_java.entity.MsgEvent;
import com.noname.lib_base_java.net.RxBus;
import com.noname.lib_base_java.util.ActivityManager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: Lmoumou
 * Date: 2019-01-10 12:01
 * 文件名: LoginActivity
 * 描述:
 */
public class LoginActivity extends BaseCenterActivity {

    @BindView(R.id.titleTb)
    Toolbar titleTb;
    @BindView(R.id.mSlidingTabLayout)
    SlidingTabLayout mSlidingTabLayout;
    @BindView(R.id.mViewPager)
    ViewPager mViewPager;

    public static void startThis(Context context) {
        Apphelper.removeAll();
        Intent intent = new Intent(context.getApplicationContext(), LoginActivity.class);
        context.startActivity(intent);
    }

    private final String[] mTitles = {"手机验证码登录", "账号密码登录"};
    private ArrayList<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {

        initToolbar(titleTb, 0, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MsgEvent msgEvent = new MsgEvent(MsgEvent.SHOW_MODLE);
                msgEvent.put("tag", 0);
                RxBus.getInstance().post(msgEvent);
                MainActivity.startThis(LoginActivity.this);
                finish();
            }
        });

        fragmentList.clear();
        fragmentList.add(PhoneLoginFragment.newInstance());
        fragmentList.add(AccountLoginFragment.newInstance());
        mViewPager.setAdapter(new PagerAdapterHome(getSupportFragmentManager(), fragmentList));
        mViewPager.setOffscreenPageLimit(2);

        mSlidingTabLayout.setViewPager(mViewPager, mTitles);
    }

    @OnClick(value = {R.id.right_tv})
    protected void onClick(View view) {
        switch (view.getId()) {
            case R.id.right_tv:
                RegisterActivity.startThis(this);
                break;
        }
    }

    @Override
    public void onBackPressed() {
//        moveTaskToBack(true);
        MsgEvent msgEvent = new MsgEvent(MsgEvent.SHOW_MODLE);
        msgEvent.put("tag", 0);
        RxBus.getInstance().post(msgEvent);
        MainActivity.startThis(LoginActivity.this);
        super.onBackPressed();
    }
}
